<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace Qiniu\Storage;

class FormUploader
{
    public static function put($upToken, $key, $data, $config, $params, $mime, $fname)
    {
        $fields = ["token" => $upToken];
        if($key === NULL) {
            $fname = "nullkey";
        } else {
            $fields["key"] = $key;
        }
        $fields["crc32"] = crc32_data($data);
        if($params) {
            foreach ($params as $k => $v) {
                $fields[$k] = $v;
            }
        }
        list($accessKey, $bucket, $err) = explodeUpToken($upToken);
        if($err != NULL) {
            return [NULL, $err];
        }
        $upHost = $config->getUpHost($accessKey, $bucket);
        $response = \Qiniu\Http\Client::multipartPost($upHost, $fields, "file", $fname, $data, $mime);
        if(!$response->ok()) {
            return [NULL, new \Qiniu\Http\Error($upHost, $response)];
        }
        return [$response->json(), NULL];
    }
    public static function putFile($upToken, $key, $filePath, $config, $params, $mime)
    {
        $fields = ["token" => $upToken, "file" => self::createFile($filePath, $mime)];
        if($key !== NULL) {
            $fields["key"] = $key;
        }
        $fields["crc32"] = crc32_file($filePath);
        if($params) {
            foreach ($params as $k => $v) {
                $fields[$k] = $v;
            }
        }
        $fields["key"] = $key;
        $headers = ["Content-Type" => "multipart/form-data"];
        list($accessKey, $bucket, $err) = explodeUpToken($upToken);
        if($err != NULL) {
            return [NULL, $err];
        }
        $upHost = $config->getUpHost($accessKey, $bucket);
        $response = \Qiniu\Http\Client::post($upHost, $fields, $headers);
        if(!$response->ok()) {
            return [NULL, new \Qiniu\Http\Error($upHost, $response)];
        }
        return [$response->json(), NULL];
    }
    private static function createFile($filename, $mime)
    {
        if(function_exists("curl_file_create")) {
            return curl_file_create($filename, $mime);
        }
        $value = "@" . $filename;
        if(!empty($mime)) {
            $value .= ";type=" . $mime;
        }
        return $value;
    }
}

?>